package com.ylb.rent.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.ylb.rent.dao.SchoolApartmentDistMapper;
import com.ylb.rent.entity.SchoolApartmentDist;
import com.ylb.rent.entity.vo.ApartmentSchoolVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.Criteria;
import org.springframework.data.elasticsearch.core.query.CriteriaQuery;
import org.springframework.data.elasticsearch.core.query.Query;
import org.springframework.stereotype.Service;
 import com.ylb.rent.dao.SchoolMapper;
import  com.ylb.rent.entity.School;
import com.ylb.rent.service.ISchoolService;

/**
 * 学校Service业务层处理
 * 
 * @author ylb
 * @date 2023-06-29
 */
@Service
public class SchoolServiceImpl implements ISchoolService 
{
    @Autowired
    private SchoolMapper schoolMapper;

    @Autowired
    private SchoolApartmentDistMapper schoolApartmentDistMapper;

    @Autowired
    ElasticsearchRestTemplate es;

    @Override
    public SearchHits<School> selectListByLikeName(String keyword) {
        Criteria criteria = new Criteria("schoolName");
        criteria.contains(keyword);
        Query query= new CriteriaQuery(criteria);
        return es.search(query, School.class);
    }

    @Override
    public List<School> selectListByApartmentId(long aId) {
        SchoolApartmentDist selector = SchoolApartmentDist.builder().apartmentId(aId).build();
        List<SchoolApartmentDist> schoolApartmentDists = schoolApartmentDistMapper.selectSchoolApartmentDistList(selector);

        List<Long> idList = schoolApartmentDists.stream().map(SchoolApartmentDist::getSchoolId).collect(Collectors.toList());
        return schoolMapper.selectBatchIds(idList);
    }

    @Override
    public List<ApartmentSchoolVo> selectVoListByApartmentId(long aId) {
        SchoolApartmentDist selector = SchoolApartmentDist.builder().apartmentId(aId).build();

//        查到该公寓下所有关联的学校
        List<SchoolApartmentDist> schoolApartmentDists = schoolApartmentDistMapper.selectSchoolApartmentDistList(selector);

//        根据集合里面的schoolid批量查到学校信息
        List<Long> idList = schoolApartmentDists.stream().map(SchoolApartmentDist::getSchoolId).collect(Collectors.toList());
        ArrayList<ApartmentSchoolVo> voList = new ArrayList<>();
        if(idList.size()>0){
            List<School> schools = schoolMapper.selectBatchIds(idList);

            for(int i=0;i<schoolApartmentDists.size();i++){
                School school = schools.get(i);
                SchoolApartmentDist schoolApartmentDist = schoolApartmentDists.get(i);

                ApartmentSchoolVo apartmentSchoolVo = new ApartmentSchoolVo(school, schoolApartmentDist);

                voList.add(apartmentSchoolVo);
            }
        }



        return voList;
    }


    /**
     * 查询学校
     * 
     * @param id 学校主键
     * @return 学校
     */
    @Override
    public School selectSchoolById(Long id)
    {
        return schoolMapper.selectSchoolById(id);
    }

    /**
     * 查询学校列表
     * 
     * @param school 学校
     * @return 学校
     */
    @Override
    public List<School> selectSchoolList(School school)
    {
        return schoolMapper.selectSchoolList(school);
    }

    /**
     * 新增学校
     * 
     * @param school 学校
     * @return 结果
     */
    @Override
    public int insertSchool(School school)
    {
        return schoolMapper.insertSchool(school);
    }

    /**
     * 修改学校
     * 
     * @param school 学校
     * @return 结果
     */
    @Override
    public int updateSchool(School school)
    {
        return schoolMapper.updateSchool(school);
    }

    /**
     * 批量删除学校
     * 
     * @param ids 需要删除的学校主键
     * @return 结果
     */
    @Override
    public int deleteSchoolByIds(Long[] ids)
    {
        return schoolMapper.deleteSchoolByIds(ids);
    }

    /**
     * 删除学校信息
     * 
     * @param id 学校主键
     * @return 结果
     */
    @Override
    public int deleteSchoolById(Long id)
    {
        return schoolMapper.deleteSchoolById(id);
    }
}
